﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    /// <summary>
    /// 收集角色
    /// </summary>
    public class UIActivityCollectionWarrior : UIActivityTemplate
    {
        public UILabel labActivityTime;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject activityItemPref;
        public UILabel topTips;
        public ActivityType type;
        protected override void PrepareData()
        {

        }

        public override void Show()
        {
            base.Show();
            CSActivityInfo msg = new CSActivityInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
        }

        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;

                activityId = msg.activityId;

                topTips.text = TableTextBlo.GetText("collection_warrior_award_tip");
                //时间
                System.DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                System.DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                labActivityTime.text = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;

                UpdateActivityItems(msg.itemList);
            }
        }

        void UpdateActivityItems(List<ActivityItem> itemList)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            for (int i = 0; i < itemList.Count; i++)
            {
                GameObject obj = Instantiate(activityItemPref);
                obj.SetActive(true);
                simpleGrid.AddChild(obj);

                var script = obj.GetComponent<AcCollectionWarriorItem>();
                script.type = type;
                script.m_callback = this.UpdateActivityItems;

                script.SetActivityItem(itemList[i], activityId);
            }

            scrollView.ResetPosition();
            if (itemList.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;

        }

    }

}
